﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PSTaskDialog;

namespace smx
{
    /// <summary>
    /// This page will be both the adding and the editing for the customers record on the database. It acheives this by
    /// having two contructors. If it is an edit form it will load the information into the form 
    /// </summary>
    public partial class customerEdit : Form
    {
       
        //holds the int for the edit data if this is an edit form otherwise remains 0 and the form is an add form
        private int id = 0;
        //add form constructor
        public customerEdit()
        {
            InitializeComponent();
        }
        //edit form constructor, takes the selected id as a paramater
        public customerEdit(string customerId)
        {
            InitializeComponent();
            id = Convert.ToInt32(customerId);
        }

        //-----------------------------------------------------------------------------------------------------------------------------
        //get the data if this is an edit form
        //-----------------------------------------------------------------------------------------------------------------------------
        private void customerEdit_Load(object sender, EventArgs e)
        {
            if (id != 0)
            {
                stockmanagerexpressEntities data = new stockmanagerexpressEntities();
                try
                {
                    var cust = from c in data.customers
                               where c.id == id
                               select c;

                    foreach (customer c in cust)
                    {
                        idTxt.Text = c.id.ToString();
                        firstnameTxt.Text = c.firstname;
                        lastnameTxt.Text = c.lastname;
                        emailTxt.Text = c.email;
                        phoneTxt.Text = c.phone;
                        lineonetxt.Text = c.addone;
                        linetwotxt.Text = c.addtwo;
                        towntxt.Text = c.town;
                        countytxt.Text = c.county;
                        postcodeTxt.Text = c.postcode;
                        notesTxt.Text = c.notes;
                        customerSinceDate.Value = (DateTime)c.dob;
                        fullnameTxt.Text = c.lastname + " , " + c.firstname;
                    }
                }
                catch (Exception err)
                {
                    cTaskDialog.MessageBox(this, "Error in Code",
                           "There was an error!",
                           "There was an error in attempting to get the record, there could be many reasons. IF the problem persists please let us know, for more information view the details" + err.Message,
                           err.ToString(), "Database error", "", eTaskDialogButtons.OK, eSysIcons.Error, eSysIcons.Error);
                }
            }
        }

        //-----------------------------------------------------------------------------------------------------------------------------
        //confirmation for closing the form
        //-----------------------------------------------------------------------------------------------------------------------------
        private void cancelBtn_Click(object sender, EventArgs e)
        {

            if (
            cTaskDialog.MessageBox(this,
                "Confirmation",
                "Are you sure you wish to cancel?",
                "Choosing yes will close this form and no data will be saved. Do you wish to proceed?",
                eTaskDialogButtons.YesNo,
                eSysIcons.Question) == DialogResult.Yes)
            {
                this.Close();
            }
        }

        //----------------------------------------------------------------------------------------------------------------------------
        //making a new record or saving the current record depending on the button press
        //----------------------------------------------------------------------------------------------------------------------------
        private void saveBtn_Click(object sender, EventArgs e)
        {
            if (id == 0)
                saveNew();
            else
                saveChanges();

            MasterForm form = (MasterForm)Application.OpenForms["MasterForm"];
            form.getCustomers();
            this.Close();
        }


        //----------------------------------------------------------------------------------------------------------------------------
        //save the changes if this is an edit form 
        //----------------------------------------------------------------------------------------------------------------------------
        private void saveChanges()
        {
            try
            {
                stockmanagerexpressEntities data = new stockmanagerexpressEntities();
                customer edit = (from c in data.customers
                                 where c.id == id
                                 select c).First();
                edit.firstname = firstnameTxt.Text;
                edit.lastname = lastnameTxt.Text;
                edit.phone = phoneTxt.Text;
                edit.email = emailTxt.Text;
                edit.addone = lineonetxt.Text;
                edit.addtwo = linetwotxt.Text;
                edit.town = towntxt.Text;
                edit.county = countytxt.Text;
                edit.postcode = postcodeTxt.Text;
                edit.notes = notesTxt.Text;
                edit.dob = (DateTime)customerSinceDate.Value;

                data.SaveChanges();
            }
            catch (Exception err)
            {
                cTaskDialog.MessageBox(this, "Error in Code",
                       "There was an error!",
                       "There was an error in attempting to edit the record, there could be many reasons. IF the problem persists please let us know, for more information view the details" + err.Message,
                       err.ToString(), "Database error", "", eTaskDialogButtons.OK, eSysIcons.Error, eSysIcons.Error);
            }
           

            
            
        }

        //----------------------------------------------------------------------------------------------------------------------------
        //save the new record to the database if this is an add form
        //----------------------------------------------------------------------------------------------------------------------------
        private void saveNew()
        {
            try
            {
                stockmanagerexpressEntities data = new stockmanagerexpressEntities();

                customer c = new customer();
                c.firstname = firstnameTxt.Text;
                c.lastname = lastnameTxt.Text;
                c.phone = phoneTxt.Text;
                c.email = emailTxt.Text;
                c.addone = lineonetxt.Text;
                c.addtwo = linetwotxt.Text;
                c.town = towntxt.Text;
                c.county = countytxt.Text;
                c.postcode = postcodeTxt.Text;
                c.notes = notesTxt.Text;
                c.dob = (DateTime)customerSinceDate.Value;

                data.AddTocustomers(c);
                data.SaveChanges();
            }
            catch (Exception err)
            {
                cTaskDialog.MessageBox(this, "Error in Code",
                       "There was an error!",
                       "There was an error in attempting to edit the record, there could be many reasons. IF the problem persists please let us know, for more information view the details" + err.Message,
                       err.ToString(), "Database error", "", eTaskDialogButtons.OK, eSysIcons.Error, eSysIcons.Error);
            }
           
        }

    }
}
